package com.rbac.modules.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rbac.common.utils.R;
import com.rbac.modules.system.dto.PermissionDTO;
import com.rbac.modules.system.dto.PermissionQueryDTO;
import com.rbac.modules.system.entity.SysPermission;
import com.rbac.modules.system.service.SysPermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "系统管理-权限管理", description = "权限管理相关接口")
@RestController
@RequestMapping("/system/permissions")
public class SysPermissionController {

    @Autowired
    private SysPermissionService permissionService;

    @Operation(summary = "获取当前用户权限树", description = "获取当前登录用户的权限树结构，用于前端动态路由")
    @GetMapping("/current/tree")
    public R<List<SysPermission>> getCurrentUserPermissionTree() {
        return R.ok(permissionService.getCurrentUserPermissionTree());
    }

    @Operation(summary = "分页查询权限")
    @GetMapping("/page")
    @SaCheckPermission(value = {"system:permission:list"})
    public R<Page<SysPermission>> page(Page<SysPermission> page, PermissionQueryDTO query) {
        return R.ok(permissionService.page(page, query));
    }

    @Operation(summary = "创建权限")
    @PostMapping
    @SaCheckPermission("system:permission:add")
    public R<Void> create(@RequestBody PermissionDTO dto) {
        permissionService.create(dto);
        return R.ok();
    }

    @Operation(summary = "更新权限")
    @PutMapping
    @SaCheckPermission("system:permission:edit")
    public R<Void> update(@RequestBody PermissionDTO dto) {
        permissionService.update(dto);
        return R.ok();
    }

    @Operation(summary = "删除权限")
    @DeleteMapping("/{id}")
    @SaCheckPermission("system:permission:delete")
    public R<Void> delete(@PathVariable Long id) {
        permissionService.delete(id);
        return R.ok();
    }

    @Operation(summary = "获取权限树")
    @GetMapping("/tree")
    @SaCheckPermission(value = {"system:permission:list"})
    public R<List<SysPermission>> tree() {
        List<SysPermission> tree = permissionService.tree();
        return R.ok(permissionService.tree());
    }

    @Operation(summary = "获取角色权限")
    @GetMapping("/role/{roleId}")
    @SaCheckPermission(value = {"system:permission:list"})
    public R<List<SysPermission>> listByRoleId(@PathVariable Long roleId) {
        return R.ok(permissionService.listByRoleId(roleId));
    }

    @Operation(summary = "获取用户权限")
    @GetMapping("/user/{userId}")
    @SaCheckPermission(value = {"system:permission:list"})
    public R<List<SysPermission>> listByUserId(@PathVariable Long userId) {
        return R.ok(permissionService.listByUserId(userId));
    }

    @Operation(summary = "更新权限状态")
    @PutMapping("/{id}/status/{status}")
    @SaCheckPermission("system:permission:edit")
    public R<Void> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        permissionService.updateStatus(id, status);
        return R.ok();
    }
}
